package com.cainezhu.coolwind.action;

import java.text.ParseException;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.cainezhu.coolwind.bean.User;
import com.cainezhu.coolwind.biz.UserService;
import com.cainezhu.coolwind.biz.impl.UserServiceImpl;
import com.cainezhu.coolwind.dao.impl.UserDaoImpl;
import com.cainezhu.coolwind.utils.SpringFactoryUtils;
import com.cainezhu.coolwind.utils.Utils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
public class UserSingInAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String uname;
	private String upwd;
	private String issave;
	private String msg;
	
	//@Autowired 
	UserService userServer;
	

	public void setUserServer(UserService userServer) {
		this.userServer = userServer;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpwd() {
		return upwd;
	}

	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}

	public String getIssave() {
		return issave;
	}

	public void setIssave(String issave) {
		this.issave = issave;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getMsg() {
		return msg;
	}

	public void setData(String msg) {
		this.msg = msg;
	}

	// @Action(value="uservalidate" , results={@Result(name="singin" ,
	// location="/info/main.jsp"),@Result(name="fault" , location=SUCCESS)})
	@Action(value = "uservalidate", results = { @Result(type = "json", params = {
			"excludeNullProperties", "true" }) })
	public String SingIn() {
		// if(issave!=null)
		this.msg = "NOMATCH";
		User user = getUserByName(uname);
		if (user != null && upwd.equals(user.getPassword())) {
			this.msg = "MATCH";
			Utils.SetSession(Utils.USER, user);
			Utils.console(this.getClass().getSimpleName(),"UNAME : "+user.getName()+" UPWD : "+user.getPassword()+" NIKENAME : "+user.getNikename()+" ISONLINE : "+user.getIsonline()+" LASTTIME : "+Utils.dateFormatToString(user.getLasttime()));
		}
		return SUCCESS;
	}
	@Action(value="login" , results={@Result(name="loginIn" ,location="/main/main.jsp"),@Result(name="timeout" ,location="/login/timeout.jsp")})
	public String loginIn(){
		if(Utils.getSession(Utils.USER)!= null){
		return "loginIn";
		}
		return "timeout";
	}
	
	public User getUserByName(String name){
		User user = null; 
		userServer = (UserService) SpringFactoryUtils.getBean("userServer", UserServiceImpl.class);
//		ApplicationContext act = SpringFactoryUtils.getApplicationContext();
//		userServer = act.getBean("userServer", UserServiceImpl.class);
		try {
			user = userServer.getUser(name);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	
	//Exit
	@Action(value="logout",results={@Result(name="logout",location="/login/logout.jsp")})
	public String loginOut(){
		if(Utils.getSession(Utils.USER)!= null)
			Utils.removeSession(Utils.USER);
		Utils.SetSession(Utils.USER, null);
		Utils.console("Exit SUCCESS!");
		return "logout";
	}
}
